#!/bin/bash
function main(){
#    hosts_ip_exist '172.22.0.2'
#    ret=$?
#    #得到返回结果
#    echo $ret
    hosts_refresh
}

#hosts是否存在某个ip
function hosts_ip_exist () {
    # IFS=$'\n\n'
    # for value in $(cat /etc/hosts)
    # do
    #     echo $value
    # done
    original_ip=$1;
    line_num=0
    while read line;  
    do
        line_num=$(($line_num+1))
        # echo $line
        ip=`echo $line | cut -f 1 -d " "`
        if [ $original_ip == $ip ]; then
            return $line_num
        fi
    done < /etc/hosts
    return 0
}

#hosts是否存在某个域名   说明liunx存在相同域名时优先级127>局域网ip>文件顺序第一个配置>其他
function hosts_domain_name_exist () {
    original_domain_name=$1;
    line_num=0
    while read line;  
    do
        line_num=$(($line_num+1))
        ip=`echo $line | cut -f 1 -d " "`
        for domain_name in $line
        do
            if [ $domain_name == $ip ]; then
                 continue
            fi
            if [ $original_domain_name == $domain_name ]; then
                return $line_num
            fi
        done
    done < /etc/hosts
    return 0
}

#写入hosts
function hosts_add () {
    ip=$1
    domain_name=$2
    is_cover=$3
    # if [ "${is_cover}" == "" ]; then
    #    is_cover="ip"
    # fi
    host=$ip
    if [ "$domain_name" != "" ]; then
        host=$host" "$domain_name
    fi
    echo "要写入的host--->"$host
    hosts=""
    if [ "$is_cover" == "ip" ] || [ "$is_cover" == "domain_name" ]; then
        while read line || [[ -n ${line} ]];  
        do
            if [ "$line" == "" ]; then
                continue
            fi
            addline=0
            curr_ip=`echo $line | cut -f 1 -d " "`
            if [ "$is_cover" == "ip" ]; then
                if [[ "${curr_ip}" != "${ip}" ]]; then
                    hosts=$hosts$line
                    addline=1
                fi
            elif [ "$is_cover" == "domain_name" ]; then
                IFS=$' '
                addip=0
                for domain in $line
                do
                    if [ $domain == $curr_ip ]; then
                        continue
                    fi
                    if [ $domain != $domain_name ]; then
                        if [[ "${addip}" == "0" ]]; then
                            hosts=$hosts$curr_ip" "$domain
                        else
                            hosts=$hosts" "$domain
                        fi
                        addip=1
                        addline=1
                    fi
                done
            fi
            if [[ "${addline}" == "1" ]]; then
                hosts=$hosts"\n"
            fi
        done < /etc/hosts
        hosts=$hosts$host
        echo -e $hosts > /etc/hosts
    else
        echo $host >> /etc/hosts
    fi
}

function docker-ip() {
    sudo docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'  $1
}

function docker-ip-list() {
    sudo docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -q)
}

#刷新hosts
function hosts_refresh() {
    run_nginx=0
    IFS=$'\n\n'
    for docker_ip in $(docker-ip-list)
    do
        docker_ip=`echo $docker_ip | cut -f 2 -d "/"`
        # echo "docker_ip-->"$docker_ip
        domain_name=`echo $docker_ip | cut -f 1 -d " "`
        # echo "domain_name-->"$domain_name
        host_ip=`echo $docker_ip | cut -f 3 -d " "`
        # echo "ip-->"$host_ip
        hosts_ip_exist $host_ip
        ret=$?
        # echo $ret
        if [ "$ret" == "0" ]; then
            hosts_add $host_ip $domain_name 'domain_name'
            run_nginx=1
        fi
    done
    if [ "$run_nginx" == "1" ]; then
        service nginx restart
    fi
}


main




